package com.yiren.manager.ctrl.messhall;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.StringUtil;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.entity.YrMealCardRechargeRecord;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.cost.YrMealCardRechargeRecordForm;
import com.yiren.manager.service.iface.cost.YrMealCardRechargeRecordService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("mealCardRechargeRecord")
@Controller
public class YrMealCardRechargeRecordController extends GenericController<YrMealCardRechargeRecordForm, YrMealCardRechargeRecord> {

	@Autowired
	private YrMealCardRechargeRecordService selfService;
	
	/**
	 * 根据日期查询充值记录
	 * @param dateSince 开始时间
	 * @param dateEnd 结束时间
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("findByDate")
	@ResponseBody
	private HTTPResult<YrMealCardRechargeRecordForm> findByDate(String dateSince,String dateEnd) throws Exception{
		HTTPResult<YrMealCardRechargeRecordForm> result = new HTTPResult<YrMealCardRechargeRecordForm>();
		String sqlWhere = "DATE_FORMAT(recharge_date,'%Y-%m-%d') >= ?1 AND DATE_FORMAT(recharge_date,'%Y-%m-%d')<=?2 AND recharge_money>0";
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("rechargeDate", "DESC");
		
		QueryResult<YrMealCardRechargeRecord> qrMealCardRechargeRecord = selfService.find(YrMealCardRechargeRecord.class, sqlWhere, new Object[]{dateSince,dateEnd},map);
		List<YrMealCardRechargeRecord> entityList = qrMealCardRechargeRecord.getResultList();
		
		ArrayList<YrMealCardRechargeRecordForm> formList = new ArrayList<YrMealCardRechargeRecordForm>();
		for(YrMealCardRechargeRecord entity : entityList){
			formList.add(toForm(entity));
		}
		result.setData(formList);
		return result;
	}
	
	/**
	 * 根据充值类型及日期查询充值记录
	 * @param dateSince 开始时间
	 * @param dateEnd 结束时间
	 * @param rechargeType 充值类型(1赠送2自主充值)
	 * @param searchVal 姓名/身份证号
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("findByDateAndRechargeType")
	@ResponseBody
	private HTTPResult<YrMealCardRechargeRecordForm> findByDate(String dateSince,String dateEnd,Integer rechargeType,String searchVal) throws Exception{
		HTTPResult<YrMealCardRechargeRecordForm> result = new HTTPResult<YrMealCardRechargeRecordForm>();
	
		String sqlWhere = "DATE_FORMAT(recharge_date,'%Y-%m-%d') >= ?1 AND DATE_FORMAT(recharge_date,'%Y-%m-%d')<=?2 AND recharge_type=?3 AND recharge_money>0";
		QueryResult<YrMealCardRechargeRecord> qrMealCardRechargeRecord = null;
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("rechargeDate", "DESC");//降序排序
		if(!StringUtil.isEmpty(searchVal)){
			sqlWhere = "(card_person_name=?4 OR card_card_id=?4) AND DATE_FORMAT(recharge_date,'%Y-%m-%d') >= ?1 AND DATE_FORMAT(recharge_date,'%Y-%m-%d')<=?2 AND recharge_type=?3 AND recharge_money>0";
			qrMealCardRechargeRecord = selfService.find(YrMealCardRechargeRecord.class, sqlWhere, new Object[]{dateSince,dateEnd,rechargeType,searchVal},map);
		}else {
			qrMealCardRechargeRecord = selfService.find(YrMealCardRechargeRecord.class, sqlWhere, new Object[]{dateSince,dateEnd,rechargeType},map);
		}
		List<YrMealCardRechargeRecord> entityList = qrMealCardRechargeRecord.getResultList();
		
		ArrayList<YrMealCardRechargeRecordForm> formList = new ArrayList<YrMealCardRechargeRecordForm>();
		for(YrMealCardRechargeRecord entity : entityList){
			formList.add(toForm(entity));
		}
		result.setData(formList);
		return result;
	}
	
	
	@Override
	public YrMealCardRechargeRecordForm toForm(YrMealCardRechargeRecord e) throws Exception {
		YrMealCardRechargeRecordForm form = ObjectUtils.objectToObject(e, YrMealCardRechargeRecordForm.class);
		return form;
	}

	@Override
	public YrMealCardRechargeRecord toEntity(YrMealCardRechargeRecordForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
